function hasClass(obj, cls) { 
    return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));  
}  

function removeClass(obj, cls) {  
    if (hasClass(obj, cls)) {  
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');  
        obj.className = obj.className.replace(reg, ' ');  
    }  
}

function removeElementByParentNode(_element){
	if(isNotEmpty( _element.parentNode)){
		 _element.parentNode.removeChild(_element);//remote this tag
	}
}

function isNotEmpty(obj){
	return obj !== null && typeof obj !== 'undefined' && obj !== '';
}

function collectionIsNotEmpty(obj){
	return isNotEmpty(obj) && obj.length > 0; 
}

function equalsIgnoreCase(a, b){
	return a.toUpperCase() === b;
}

function removeAdvertisingTags(needRemoveTags){
	//remove advertising tag
	var body = document.getElementsByTagName("body")[0];
	body.childNodes.forEach(function(_element){
		needRemoveTags.forEach(function(tag){
			if(isNotEmpty(_element.tagName) && equalsIgnoreCase(_element.tagName, tag)){
				body.removeChild(_element);//remote this tag
			}
		});
	});
}

function doRemoveAdvTagsByCalss(clazz){
	var _elements = document.getElementsByClassName(clazz);
	if(collectionIsNotEmpty(_elements)){
		for(var index = 0; index < _elements.length; index++){
			removeElementByParentNode(_elements[index]);
		}
	}
}

function removeAdvertisingTagsByClass(needRemoveTagsClass, defaultZIndex){
	//remove advertising tag
	needRemoveTagsClass.forEach(function(clazz){
		var removeAdvInterval = setInterval(function(){
			doRemoveAdvTagsByCalss(clazz);
			removeZindexGreaterThanX(defaultZIndex);
		},100);
		
		setTimeout(function(){
			clearInterval(removeAdvInterval);
		},10000);
	});
}

function removeZindexGreaterThanX(defaultZIndex, targetTagName){
	//remove z-index > 1 tag
	var target = document.getElementsByTagName(targetTagName);
	var _element;
	for(var index = 0; index < target.length; index++){
		_element = target[index];
		if(isNotEmpty(_element)  && _element.style.zIndex > defaultZIndex){
			removeElementByParentNode(_element);
		}
	}
}

var DIV_TAG= "div", HYPER_LINK_TAG= "a";
var DEFAULT_REMOVE_TAGS = ['A'];
var DEFAULT_ZINDEX = 0;
var DEFAULT_REMOVE_TAGS_CLASS = ['J_adv','google-ad','adsbygoogle'];

function _clearAdvertising(){
	
	removeZindexGreaterThanX(DEFAULT_ZINDEX, DIV_TAG);
	
	removeZindexGreaterThanX(DEFAULT_ZINDEX, HYPER_LINK_TAG);

	removeAdvertisingTags(DEFAULT_REMOVE_TAGS);

	removeAdvertisingTagsByClass(DEFAULT_REMOVE_TAGS_CLASS, DEFAULT_ZINDEX);
}

document.onreadystatechange = function(){//当页面加载状态改变的时候执行这个方法. 
	
	var readyState = document.readyState, _body = $("body");
	
	if(readyState == 'complete') {
		_clearAdvertising();
		_body.fadeIn();
	}else if(readyState == 'interactive') {
		_body.fadeOut();
		_clearAdvertising();//第一次清理广告块
	}
};